/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.manager;

import entity.Construct;
import entity.Cost;
import entity.Ownership;
import entity.OwnershipPK;
import entity.Player;
import entity.ProductionCost;
import entity.Storage;
import entity.StoragePK;
import exception.ValueNotFoundException;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Giuseppe
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)//La transazione è gestita dal container
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class CheckBean implements CheckBeanLocal {
    
    @PersistenceContext(unitName = "BrowserGame-ejbPU")
    private EntityManager entityManager;

    @Override
    public boolean checkEra(String username, String constructName) {
        System.out.print(username + " " + constructName);
        Construct construct = entityManager.find(Construct.class, constructName);
        Player player = entityManager.find(Player.class, username);
        if(player == null || construct == null) {
            System.out.print(player + " " + construct);
            throw new ValueNotFoundException("username e/o costrutto non trovati");
        }
        return construct.getEraCollection().contains(player.getEra());
    }

    @Override
    public boolean checkSlot(String username, int amount) {
        Player player = entityManager.find(Player.class, username);
        Collection<Ownership> ownerships = player.getOwnershipCollection();
        int count = amount;
        for (Ownership ownership : ownerships) {
            count += ownership.getAmount();
        }
        return count <= player.getEra().getSlot();
    }

    @Override
    public boolean chekCost(String username, String constructName, int amount) {
        final String stringQuery = "SELECT c.amount, s.amount " + 
            "FROM " + Cost.class.getSimpleName() + " c, " + Storage.class.getSimpleName() + " s " + 
            "WHERE c.costPK.construct = ?1 " + 
            "AND s.storagePK.player = ?2 " + 
            "AND s.storagePK.resource = c.costPK.resource " +
            "AND s.amount < c.amount * ?3";
        Query query = entityManager.createQuery(stringQuery);
        query.setParameter(1, constructName);
        query.setParameter(2, username);
        query.setParameter(3, amount);
        return query.getResultList().isEmpty();
    }

    @Override
    public boolean checkProductionCost(String username, String constructName, int amount) {
        final String stringQuery = "SELECT pc.amount, s.amount " + 
            "FROM " + ProductionCost.class.getSimpleName() + " pc, " + Storage.class.getSimpleName() + " s " + 
            "WHERE pc.productionCostPK.construct = ?1 " + 
            "AND s.storagePK.player = ?2 " + 
            "AND s.storagePK.resource = pc.productionCostPK.resource " +
            "AND s.amount < pc.amount * ?3";
        Query query = entityManager.createQuery(stringQuery);
        query.setParameter(1, constructName);
        query.setParameter(2, username);
        query.setParameter(3, amount);
        return query.getResultList().isEmpty();
    }
    
    @Override
    public boolean checkConstruct(String username, String constructName, int amount) {
        OwnershipPK ownershipPK = new OwnershipPK(username, constructName);
        Ownership ownership = entityManager.find(Ownership.class, ownershipPK);
        return (ownership.getAmount() - ownership.getOccupied()) >= amount;
    }

    @Override
    public boolean checkResourceAmount(String username, String resource, int amount) {
        StoragePK pk = new StoragePK(username, resource);
        Storage storage = entityManager.find(Storage.class, pk);
        return storage.getAmount() >= amount;
    }
      
}
